from PyQt6.QtCore import QRect
from PyQt6.QtWidgets import QGroupBox, QHBoxLayout, QPushButton, QLabel, QWidget, QVBoxLayout

from .image_label import ImageLabel


class ImageGroupBox(QGroupBox):
    """图片展示区域"""

    def __init__(self, parent=None):
        super().__init__(parent)

        # 创建垂直布局管理器
        self.mainLayout = QVBoxLayout(self)

        # 图片标签
        self.imgLabel = ImageLabel(self)
        self.mainLayout.addWidget(self.imgLabel)

        # 连接ImageLabel的信号到处理函数
        self.imgLabel.selectionChanged.connect(self.on_selection_changed)
        # 连接图片加载信号
        self.imgLabel.imageLoaded.connect(self.on_image_loaded)

        # 按钮区域
        self.buttonWidget = QWidget(self)
        self.buttonLayout = QHBoxLayout(self.buttonWidget)

        self.enlargeBtn = QPushButton("放大", self.buttonWidget)
        self.ReduceBtn = QPushButton("缩小", self.buttonWidget)
        self.resetBtn = QPushButton("重置", self.buttonWidget)
        self.fitWindowsBtn = QPushButton("适应窗口", self.buttonWidget)
        self.imageScalingRatioLabel = QLabel("当前缩放：1.0X", self.buttonWidget)

        self.buttonLayout.addWidget(self.enlargeBtn)
        self.buttonLayout.addWidget(self.ReduceBtn)
        self.buttonLayout.addWidget(self.resetBtn)
        self.buttonLayout.addWidget(self.fitWindowsBtn)
        self.buttonLayout.addWidget(self.imageScalingRatioLabel)

        self.mainLayout.addWidget(self.buttonWidget)

        # 设置拉伸因子，使图片区域占据更多空间
        self.mainLayout.setStretch(0, 1)  # 图片区域
        self.mainLayout.setStretch(1, 0)  # 按钮区域

    def on_selection_changed(self, pixmap, width, height, x, y, record_pos, name):
        """处理选区更改事件"""
        # 这里可以添加处理选区更改的逻辑
        # 目前我们只是打印信息以确认信号正常工作
        print(f"Selection changed - Width: {width}, Height: {height}, X: {x}, Y: {y}")
        
    def on_image_loaded(self, width, height):
        """处理图片加载事件"""
        try:
            # 这里可以添加处理图片加载的逻辑
            print(f"Image loaded - Width: {width}, Height: {height}")
        except Exception as e:
            print(f"图片加载时发生错误: {e}")


from PyQt6.QtWidgets import QFormLayout, QSpinBox, QLineEdit, QPushButton, QVBoxLayout

class CaptureInfoBox(QGroupBox):
    def __init__(self, parent=None):
        super().__init__(parent)

        # 创建布局
        layout = QVBoxLayout(self)

        # 截图尺寸
        size_layout = QFormLayout()
        self.width_input = QSpinBox()
        self.width_input.setMaximum(5000)
        self.height_input = QSpinBox()
        self.height_input.setMaximum(5000)
        size_layout.addRow("宽度", self.width_input)
        size_layout.addRow("高度", self.height_input)
        layout.addLayout(size_layout)

        # 坐标
        coord_layout = QFormLayout()
        self.x_input = QSpinBox()
        self.y_input = QSpinBox()
        coord_layout.addRow("X坐标", self.x_input)
        coord_layout.addRow("Y坐标", self.y_input)
        layout.addLayout(coord_layout)

        # 其他信息
        other_layout = QFormLayout()
        self.record_pos_input = QLineEdit()
        self.record_pos_input.setReadOnly(True)
        self.name_input = QLineEdit()
        other_layout.addRow("recordPos", self.record_pos_input)
        other_layout.addRow("截图名称", self.name_input)
        layout.addLayout(other_layout)

        # 按钮
        button_layout = QVBoxLayout()
        self.save_button = QPushButton("保存截图")
        self.test_button = QPushButton("测试模板")
        button_layout.addWidget(self.save_button)
        button_layout.addWidget(self.test_button)
        layout.addLayout(button_layout)

    def update_info(self, width, height, x, y, record_pos, name):
        """更新截图信息"""
        self.width_input.setValue(width)
        self.height_input.setValue(height)
        self.x_input.setValue(x)
        self.y_input.setValue(y)
        self.record_pos_input.setText(record_pos)
        self.name_input.setText(name)